package cn.tedu.ivos.base.util;

import java.security.SecureRandom;

public class PasswordUtil {
    private static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*.-_=+";
    //创建一个安全的随机生成器，用于生成随机密码
    private static final SecureRandom random = new SecureRandom();

    public static String generatePassword(int length) {
        //参数校验
        if(length<5)throw new RuntimeException("密码长度不能小于5位！");
        StringBuilder password = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(ALLOWED_CHARS.length());
            char randomChar = ALLOWED_CHARS.charAt(index);
            password.append(randomChar);
        }
        return password.toString();
    }
}
